/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package program.utilities;

import java.io.*;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import program.beans.Bill;
import program.beans.Category;

/**
 * Utility-class which writes a statistic out of bills to a pdf-file
 *
 * Responsible: Bernhard
 */
public class StatisticUtilities {

    /**
     * Generates a statistic out of delivered bills and writes it to a pdf-file
     * with assistance of the PDFUtilities-Class
     *
     * @param bills
     * @param cats
     * @param from
     * @param since
     * @return statistic pdf-file
     */
    public static File createStatistic(ArrayList<Bill> bills, ArrayList<Category> cats, Date from, Date since) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
        String dateFrom = sdf.format(from);
        String dateSince = sdf.format(since);
        String file = getTemplate();
        String tableIn = createTable("Eingangsrechnungen", bills, cats, 1);
        String tableOut = createTable("Ausgangsrechnungen", bills, cats, 0);
        file = file.replaceAll("#billFrom", dateFrom);
        file = file.replaceAll("#billSince", dateSince);
        file = file.replaceAll("#billsEarining", tableIn);
        file = file.replaceAll("#billsOutgoing", tableOut);
        file = file.replaceAll("€", "&euro;");

        File f = File.createTempFile("statistic", ".html");
        FileWriter fw = new FileWriter(f);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(file);
        bw.newLine();
        bw.close();

        File statistic = PDFUtilities.createPDF(f);

        return statistic;
    }

    private static String getTemplate() throws Exception {
        String template = "";
        File f = new File("templates\\printbill.html");
        FileReader fr = new FileReader(f);
        BufferedReader br = new BufferedReader(fr);

        while (br.ready()) {
            template += br.readLine();
        }

        br.close();

        return template;
    }

    /**
     * Creates a html-table out of a delivered title, delivered bills, delivered
     * categories and a delivered billType
     *
     * @param title
     * @param bills
     * @param cats
     * @param billType
     * @return html-table
     */
    private static String createTable(String title, ArrayList<Bill> bills, ArrayList<Category> cats, int billType) {
        String table = "";

        table += "<p class=\"auto-style2\"><strong><em>" + title + "</em></strong></p>" + "\n";
        table += "<table cellspacing=\"0\" class=\"auto-style3\" style=\"width: 100%\">" + "\n";
        table += "<tr>" + "\n";
        table += "<td class=\"auto-style5\"><em><strong>Datum</strong></em></td>" + "\n";
        table += "<td class=\"auto-style5\"><em><strong>Firma</strong></em></td>" + "\n";
        table += "<td class=\"auto-style5\"><em><strong>Bruttobetrag</strong></em></td>" + "\n";
        table += "<td class=\"auto-style5\"><em><strong>Nettobetrag</strong></em></td>" + "\n";
        table += "</tr>" + "\n";


        String help = "";
        boolean wasSomethingInserted = false;
        for (Category c : cats) {
            help += "<tr>" + "\n";
            help += "<td class=\"auto-style6\" colspan=\"5\"><strong>Kategorie: " + c.getDescription() + "</strong></td>" + "\n";
            help += " </tr>" + "\n";

            BigDecimal sumBrutto = new BigDecimal(0);
            BigDecimal sumNetto = new BigDecimal(0);
            int count = 0;

            for (Bill b : bills) {
                if (b.getCat().equals(c) && b.getType() == billType) {
                    count++;
                    wasSomethingInserted = true;
                    help += "<tr>" + "\n";
                    help += "<td class=\"auto-style4\"> " + b.getBillDate() + "</td>";
                    help += "<td class=\"auto-style4\">" + b.getComp().getName() + "</td>";
                    help += "<td class=\"auto-style4\">" + b.getAllBruttoAmountsFormated() + "</td>";
                    help += "<td class=\"auto-style4\">" + b.getAllNettoAmountsFormated() + "</td>";
                    help += "</tr>";

                    sumBrutto = sumBrutto.add(b.getAllBruttoAmounts());
                    sumNetto = sumNetto.add(b.getAllNettoAmounts());
                }
            }

            help += "<tr>";
            help += "<td class=\"auto-style4\" colspan=\"2\">Summe:</td>";
            help += "<td class=\"auto-style4\">" + NumberFormat.getCurrencyInstance().format(sumBrutto.doubleValue()) + "</td>";
            help += "<td class=\"auto-style4\">" + NumberFormat.getCurrencyInstance().format(sumNetto.doubleValue()) + "</td>";
            help += "</tr>";

            if (count > 0) {
                table += help;
            }
            help = "";


        }
        table += "</table>";

        if (wasSomethingInserted) {
            return table;
        } else {
            return "";
        }
    }
}
